package com.itboye.jmsfhz.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.EnumUtility;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.adapter.ChangwuLishiZhangAdapter;
import com.itboye.jmsfhz.app.adapter.FuLishiZhangAdapter;
import com.itboye.jmsfhz.app.adapter.LishiZhangAdapter;
import com.itboye.jmsfhz.app.adapter.ZuZhiJiGouAdapter;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.data.model.HomePageModel;
import com.itboye.jmsfhz.data.model.LunBoModel;
import com.itboye.jmsfhz.data.presenter.LishiZhangPresenter;
import com.itboye.jmsfhz.data.presenter.LunBoPresenter;
import com.itboye.jmsfhz.data.presenter.Personnelpresenter;
import com.itboye.jmsfhz.widget.CarouselView;
import com.itboye.jmsfhz.widget.XScrollView;
import com.lfh.commentdemo.bean.AppModelNewsBean;
import com.lfh.commentdemo.view.MyListView;

public class ZuZhiJiGOuFragment extends BaseFragment implements Observer {

	CarouselView topLunbo;
	public static XScrollView xScrollView;
	private List<ImageView> bmps;
	LunBoPresenter lunBoPresenter;
	MyListView myzuzhilistviewnews, myzuzhilistviewmessge, myzuzhilistviephout;

	private LunBoModel Beannermodel;

	View view;

	private String page_num = 1 + "";
	private String page_size = 10 + "";
	private AppModelNewsBean bean;
	private LishiZhangPresenter personnelpresenter;
	BaseAdapter adapter;
	int position;
	int i = 0;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.zuzhijiegoufragment;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		xScrollView.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				xScrollView.scrollTo(0, 0);
			}
		});
		Beannermodel = new LunBoModel();
		lunBoPresenter = new LunBoPresenter(this);
		Beannermodel.setPage_num(page_num + "");
		Beannermodel.setPage_size(page_size);
		Beannermodel.setPosition(6045);
		lunBoPresenter.getInfo(Beannermodel);

		onLiSiZhang();
		onChangwuLiSiZhang();
		onFuWuLiSiZhang();
	}

	// 理事长
	public void onLiSiZhang() {
		int page_size = 5;
		bean = new AppModelNewsBean();
		personnelpresenter = new LishiZhangPresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.LISHIZ.getValue() + "");
		personnelpresenter.add(bean);
		myzuzhilistviewnews.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub http://122.224.163.226:8080/index.php/Webview/Post/viewPost?id=
				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "理事长");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(position).getId());
				intent.putExtra("cate", bean.getCate());
				intent.putExtra("id", bean.getList().get(position).getId());
				bean.setCate(EnumUtility.LISHIZ.getValue() + "");
				startActivity(intent);
			}
		});
	}

	public void onChangwuLiSiZhang() {
		int page_size = 5;
		bean = new AppModelNewsBean();
		personnelpresenter = new LishiZhangPresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.CHANGWULISHIZ.getValue() + "");
		personnelpresenter.getInfo(bean);

		myzuzhilistviewmessge.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "常务理事长");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(position).getId());
				intent.putExtra("cate", bean.getCate());
				intent.putExtra("id", bean.getList().get(position).getId());
				bean.setCate(EnumUtility.CHANGWULISHIZ.getValue() + "");
				startActivity(intent);
			}
		});
	}

	public void onFuWuLiSiZhang() {
		
		
		int page_size = 5;
		bean = new AppModelNewsBean();
		personnelpresenter = new LishiZhangPresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.FULISHIZ.getValue() + "");
		personnelpresenter.update(bean);
		
		
		myzuzhilistviephout.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "副理事长");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(position).getId());
				intent.putExtra("cate", bean.getCate());
				intent.putExtra("id", bean.getList().get(position).getId());
				bean.setCate(EnumUtility.FULISHIZ.getValue() + "");
				startActivity(intent);
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity model = super.handlerError(data);
		//理事长
		if (model.getEventType() == personnelpresenter.LishiSuccessEvent) {

			bean = (AppModelNewsBean) model.getData();
			LishiZhangAdapter	 adapter = new LishiZhangAdapter(getActivity(),
					bean);
			myzuzhilistviewnews.setAdapter(adapter);
			xScrollView.smoothScrollTo(0, 0);
		}
		// 常务
		if (model.getEventType() == personnelpresenter.changwuSuccessEvent) {

			bean = (AppModelNewsBean) model.getData();
			ChangwuLishiZhangAdapter	 adapter = new ChangwuLishiZhangAdapter(getActivity(),
					bean);
			myzuzhilistviewmessge.setAdapter(adapter);
			xScrollView.smoothScrollTo(0, 0);
		}//副的
		if (model.getEventType() == personnelpresenter.FuSuccessEvent) {
			bean = (AppModelNewsBean) model.getData();
			FuLishiZhangAdapter	 adapter = new FuLishiZhangAdapter(getActivity(),
					bean);
			myzuzhilistviephout.setAdapter(adapter);
			xScrollView.smoothScrollTo(0, 0);
		}// 轮播
		if (model.getEventType() == lunBoPresenter.BannerSuccessEvent) {
			Beannermodel = (LunBoModel) model.getData();
			bmps = new ArrayList<ImageView>();
			for ( i = 0; i < Beannermodel.getResult().size(); i++) {
				position = i;
				ImageView imageView = new ImageView(getActivity());

				imageView.setScaleType(ScaleType.FIT_XY);
				com.nostra13.universalimageloader.core.ImageLoader
						.getInstance().displayImage(
								GlobalConfig.JIUYE
										+ Beannermodel.getResult().get(i)
												.getImg(), imageView);
//				imageView.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View arg0) {
//						// TODO Auto-generated method stub
//						System.out.println(">>>>>>>>>>>>>>>>>>title"+i);
//						
//						if(Beannermodel.getResult().get(0).getUrl()==null ||Beannermodel.getResult().get(0).getUrl()==""){
//						}else{
//							Intent intent=new Intent(getActivity(),WebActivity.class);
//							intent.putExtra("url",GlobalConfig.WEBURL +Beannermodel.getResult().get(0).getUrl());
//							startActivity(intent);
//						}
//						
//					}
//				});
				bmps.add(imageView);

			}

			topLunbo.setImageBitmaps(bmps);

		}
	}
}
